<?php

namespace app\api\controller;

use app\api\model\Article as ArticleModel;
use app\api\model\article\Category as CategoryModel;
use app\common\model\Introduction;
use app\common\model\introduction\Category as Cate;
use app\store\model\Article as newsModel;
use think\Db;
use think\Log;
/**
 * 文章控制器（资讯）+ 关于我们
 * Class Article
 * @package app\api\controller
 */
class Article extends Controller
{
    private $user;

    /**
     * 构造方法
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function _initialize()
    {
        parent::_initialize();
        // $this->user = $this->getUser();
    }

    /**
     * 文章首页
     * @return array
     */
    public function index()
    {
        // 文章分类列表
        $categoryList = CategoryModel::getAll();
        return $this->renderSuccess(compact('categoryList'));
    }

    /**
     * 文章列表
     * @param int $category_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function lists($team_id = 0,$user_id = 0)
    {
        $user = $this->getUser();
        $shanghui_id = $user["shanghui_id"];//当前用户的商会id
        if ($user['grade_id'] == 10008) {//此条为所谓秘书长即总管理发布活动
            $team_id = 0;//为代表处活动
        }
        if ($user['grade_id'] == 10009) {//此条为所谓秘书长+分队管理布活动
            $user_id = 1;//为双权限
        }
        $model = new ArticleModel;
        $list = $model->getList($team_id,$user_id, $shanghui_id);
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 文章详情
     * @param $article_id
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function detail($article_id)
    {
        $detail = ArticleModel::detail($article_id);
        return $this->renderSuccess(compact('detail'));
    }

    /**
     * 添加/编辑文章
     */
    public function edit()
    {
        $param = input('param.');
        $user = $this->getUser();
        $param['team_id'] = $user['team_id'];
        $param['user_id'] = $user['user_id'];
        $param['shanghui_id'] = $user['shanghui_id'];
        if ($user['grade_id'] == 10008) {//此条为所谓秘书长即总管理发布活动
            $param['team_id'] = 0;//为代表处活动
        }
        if ($user['grade_id'] == 10009 && $param['location'] == 1) {//此条为所谓秘书长即总管理发布活动
            $param['team_id'] = 0;//为代表处活动
        }
        if (!$param['article_id']){
            // 新增记录
            $model = new newsModel;
            $res = $model->add($param);
            if ($res) {
                return $this->renderSuccess('添加成功');
            }
            return $this->renderError($model->getError() ?: '添加失败');
        }
        $model = newsModel::detail($param['article_id']);
        $res = $model->edit($param);
        if ($res) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /*********************************************************关于我们相关**************************************************************
     * 关于我们的分类LIST
     */
    public function aboutuscate()
    {
        $user = $this->getUser();
        $list = Cate::getALL($user['shanghui_id']);
        return $this->renderSuccess(compact('list'));
    }
    /**
     * 关于我们（普通用户查看）
     * @param $cate_id
     * @return array
     */
    public function aboutus($cate_id)
    {
        $detail = Introduction::where("category_id", '=', $cate_id)->where('is_delete',0)->find();
        $detail['introduction_content'] = html_entity_decode($detail['introduction_content']);
        $detail['category'] = Db::name('introduction_category')->where('category_id',$cate_id)->find();
        return $this->renderSuccess(compact('detail'));
    }

    /**
     * 关于我们编辑
     * @return array
     */
    public function aboutus_edit(){
        $param = input('param.');
        $user = $this->getUser();
        $param['shanghui_id'] = $user['shanghui_id'];
        // 新增记录
        if (!$param['category_id']){
            // 新增cate记录
            $cdata = [
                'shanghui_id'=>$user['shanghui_id'],
                "sort" => $param['sort'],
                "name" => $param['name'],
                "wxapp_id" => $param['wxapp_id'],
                "create_time" => time(),
                "update_time" => time(),
            ];
            $cmodel = new Cate;
            $res = $cmodel->save($cdata);
            if($res){
                // 内容表
                $idata=[
                    "wxapp_id" => $param['wxapp_id'],
                    "create_time" => time(),
                    "update_time" => time(),
                    'shanghui_id'=>$user['shanghui_id'],
                    'introduction_content'=>$param['introduction_content'],
                    'category_id'=>Db::name('introduction_category')->order("category_id desc")->value('category_id')
                ];
                $model = new Introduction;
                $res = $model->save($idata);
                if ($res) {
                    return $this->renderSuccess([],'添加成功');
                }
            }
            return $this->renderError('添加失败');
        }
        $cdata = [
            "sort" => $param['sort'],
            "name" => $param['name'],
            "update_time" => time(),
        ];
        // 编辑记录(cate)
        cate::where('category_id',$param['category_id'])->update($cdata);
        // 内容表
        $idata=[
            "update_time" => time(),
            'introduction_content'=>$param['introduction_content']
        ];
        $res = Introduction::where('category_id',$param['category_id'])->update($idata);
        return $this->renderSuccess([],'更新成功');
    }
     /**
     * 关于我们删除
     * @return array
     */
    public function aboutus_del($category_id){
        cate::where('category_id',$category_id)->delete();
        Introduction::where('category_id',$category_id)->delete();
        return $this->renderSuccess([],'删除成功');
    }


}
